import dayjs from 'dayjs';
import type { ConfigType } from 'dayjs';
import isToday from 'dayjs/plugin/isToday';
import isTomorrow from 'dayjs/plugin/isTomorrow';
import relativeTime from 'dayjs/plugin/relativeTime';
import 'dayjs/locale/zh-cn';

dayjs.locale('zh-cn'); // 全局使用简体中文
dayjs.extend(isToday);
dayjs.extend(isTomorrow);
dayjs.extend(relativeTime);

const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss';
const DATE_FORMAT = 'YYYY-MM-DD ';
export function formatToDateTime(date: ConfigType, format = DATE_TIME_FORMAT) {
  if (typeof date === 'number') {
    return dayjs.unix(date).format(format);
  }
  return dayjs(date).format(format);
}

export function formatToDate(date: ConfigType, format = DATE_FORMAT) {
  return dayjs(date).format(format);
}

export function formatWeek(date: ConfigType) {
  if (dayjs(date).isToday()) {
    return '今天';
  }
  if (dayjs(date).isTomorrow()) {
    return '明天';
  }
  return dayjs(date).format('ddd');
}

export function fromNowDate(date: ConfigType) {
  return dayjs(date).fromNow();
}
